<?php 
/*
 module:		留言管理
 create_time:	2022-03-08 10:37:29
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\backend\service\ZhMessageService;
use app\backend\model\ZhMessage as ZhMessageModel;
use think\facade\Db;

class ZhMessage extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['status'] = $this->request->param('status', '', 'serach_in');
            $where['wxapp_id'] = session('backend.wxapp_id');
			$createtime_start = $this->request->param('createtime_start', '', 'serach_in');
			$createtime_end = $this->request->param('createtime_end', '', 'serach_in');

			$where['createtime'] = ['between',[strtotime($createtime_start),strtotime($createtime_end)]];

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'message_id,content,status,createtime';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'message_id desc';

			$res = ZhMessageService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}

	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$message_id = $this->request->get('message_id','','serach_in');
			if(!$message_id) $this->error('参数错误');
            $zhMessageData = checkData(ZhMessageModel::find($message_id));
            $zhMessageData['images'] = explode(',',$zhMessageData['images']);
			$this->view->assign('info',$zhMessageData);
			return view('update');
		}else{
			$postField = 'message_id,wxapp_id,content,status,createtime,images';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = ZhMessageService::update($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*删除*/
	function delete(){
		$idx =  $this->request->post('message_id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
			ZhMessageModel::destroy(['message_id'=>explode(',',$idx)],true);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看详情*/
	function view(){
		$message_id = $this->request->get('message_id','','serach_in');
		if(!$message_id) $this->error('参数错误');
        $zhMessageData = ZhMessageModel::find($message_id);
        $zhMessageData['images'] = explode(',',$zhMessageData['images']);
		$this->view->assign('info',$zhMessageData);
		return view('view');
	}



}

